package com.wpw.dream.copy;

public class Student implements Cloneable {
	
	private String name;
	
	private School school;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	protected Object clone() {
		Student s = null;
		try {
			s = (Student) super.clone();
			if (s.getSchool() != null) {
				s.setSchool((School) s.getSchool().clone());
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return s;
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", school=" + school + "]";
	}

	public static void main(String[] args) {
		Student xiaoming = new Student();
		xiaoming.setName("小明");
		School sc = new School();
		sc.setName("一小");
		xiaoming.setSchool(sc);
		
		Student xiaohong = (Student) xiaoming.clone();
		xiaohong.setName("小红");
		xiaohong.getSchool().setName("二小");
		System.out.println(xiaoming);
		System.out.println(xiaohong);
	}
}